//
//  HDMessageViewController.m
//  HDecoration
//
//  Created by 曹群 on 15/8/15.
//  Copyright (c) 2015年 HDecoration. All rights reserved.
//

#import "HDMessageViewController.h"
#import "HDMessageTableViewController.h"

@interface HDMessageViewController ()
@property(nonatomic,strong)GXButton *deleteBtn;
@property(nonatomic,strong)HDMessageTableViewController *messageList;
@end

@implementation HDMessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kContentViewColor;
    [self.view addSubview:self.messageList.tableView];
}

- (void)initNavigationBar
{
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -10;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, [[UIBarButtonItem alloc] initWithCustomView:self.deleteBtn], nil];
}

- (void)pullTableView:(UITableView *)tableView isNextPage:(BOOL)isNextPage
{
    
}

- (void)pullRefreshRequest
{
    [self testDataFunc];
}

- (void)testDataFunc
{
    NSDictionary* params=@{
                           @"accept_id":netEngine.coachInfo.m_id,
                         };
    [netEngine RequestMsgListWithTarget:self.VctrID hudView:self.messageList.tableView Data:params];
}

-(void)updateViewController:(NSNotification *)note
{
    NSDictionary* dic=note.userInfo;
    if ([dic objectForKey:Receiver]==self.VctrID)
    {
        if (dic[@"MsgList"]!=nil)
        {
            self.messageList.dataArray =(NSMutableArray*)dic[@"data"];
        }
        else if (dic[@"DeleteMsgList"]!=nil)
        {
            self.messageList.dataArray =nil;
        }
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==110&&buttonIndex==1)
    {
            NSDictionary* params=@{
                                      @"accept_id":netEngine.coachInfo.m_id,
                                 };
        
            [netEngine RequestDeleteMsgListWithTarget:self.VctrID hudView:self.messageList.tableView Data:params];
    }
}


- (void)deleteMsg:(UIButton *)sender
{
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:@"清空消息列表" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    alert.tag=110;
    [alert show];
}

#pragma mark - getters
- (UIButton *)deleteBtn
{
    if (!_deleteBtn) {
        _deleteBtn = [GXButton buttonWithType:UIButtonTypeCustom];
        _deleteBtn.frame = CGRectMake(0, 0, 40, 40);
        _deleteBtn.imageViewFrame = CGRectMake(12, 9, 16, 22);
        _deleteBtn.relayout = YES;
        [_deleteBtn setImage:[UIImage imageNamed:@"删除"] forState:UIControlStateNormal];
        [_deleteBtn addTarget:self action:@selector(deleteMsg:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _deleteBtn;
}

- (HDMessageTableViewController *)messageList
{
    if (!_messageList) {
        _messageList = [[HDMessageTableViewController alloc] initWithStyle:UITableViewStyleGrouped];
        _messageList.tableView.frame = CGRectMake(0, 64, APPFRAME.size.width, APPFRAME.size.height-44);
        _messageList.delegate=(id)self;
        _messageList.enableRefresh = YES;
        _messageList.enableRequest = NO;
    }
    return _messageList;
}

@end
